package ynu.edu.achitecturework.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import ynu.edu.achitecturework.common.result.Result;
import ynu.edu.achitecturework.entity.CameraData;
import ynu.edu.achitecturework.service.CameraService;
import ynu.edu.achitecturework.service.YsService;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/camera")
@RequiredArgsConstructor
@Slf4j
public class CameraController {

    private final CameraService cameraService;

    private final YsService ysService;

    @GetMapping("/{deviceId}/photos")
    public Result<List<CameraData>> getRecentPhotos(@PathVariable Long deviceId) {
        log.info("获取设备 {} 的最近照片", deviceId);
        try {
            List<CameraData> photos = cameraService.getRecentPhotos(deviceId);
            log.info("找到 {} 张照片", photos.size());
            return Result.success(photos);
        } catch (Exception e) {
            log.error("获取照片失败", e);
            return Result.error(500, "获取照片失败: " + e.getMessage());
        }
    }

    @GetMapping("token")
    public Result<String> getToken() {
        return Result.success(ysService.getAccessToken());
    }
}